<?php

namespace App\Entity\Wuliu;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\User;
use App\Entity\Wuliu\WuliuUserLevel;
use App\Repository\Wuliu\WuliuUserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=WuliuUserRepository::class)
 */
class WuliuUser
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"user"})
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity=User::class, cascade={"persist", "remove"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;

    /**
     * @ORM\ManyToOne(targetEntity=WuliuUserLevel::class, inversedBy="wuliuUsers")
     * @ORM\JoinColumn(nullable=false)
     * @Groups({"user","level"})
     */
    private $level;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @Groups({"user"})
     */
    private $expireAt;

    /**
     * @ORM\OneToMany(targetEntity=WuliuOrder::class, mappedBy="user")
     */
    private $wuliuOrders;

    public function __construct()
    {
        $this->wuliuOrders = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(User $user): self
    {
        $this->user = $user;

        return $this;
    }

    public function getLevel(): ?WuliuUserLevel
    {
        return $this->level;
    }

    public function setLevel(?WuliuUserLevel $level): self
    {
        $this->level = $level;

        return $this;
    }

    public function getExpireAt(): ?\DateTimeInterface
    {
        return $this->expireAt;
    }

    public function setExpireAt(?\DateTimeInterface $expireAt): self
    {
        $this->expireAt = $expireAt;

        return $this;
    }

    /**
     * @return Collection|WuliuOrder[]
     */
    public function getWuliuOrders(): Collection
    {
        return $this->wuliuOrders;
    }

    public function addWuliuOrder(WuliuOrder $wuliuOrder): self
    {
        if (!$this->wuliuOrders->contains($wuliuOrder)) {
            $this->wuliuOrders[] = $wuliuOrder;
            $wuliuOrder->setUser($this);
        }

        return $this;
    }

    public function removeWuliuOrder(WuliuOrder $wuliuOrder): self
    {
        if ($this->wuliuOrders->contains($wuliuOrder)) {
            $this->wuliuOrders->removeElement($wuliuOrder);
            // set the owning side to null (unless already changed)
            if ($wuliuOrder->getUser() === $this) {
                $wuliuOrder->setUser(null);
            }
        }

        return $this;
    }
}
